#!/bin/sh
#
# Usage:
#   ./3-tools.sh <function name>

set -o nounset

. $PWD/util.sh
. $PWD/2-tools.sh  # for gcc

kernel_headers() {
  maybe_untar_cd linux-3.10.10 .tar.xz

  # would do this above except for the if statement.
  set -o errexit

  cd linux-3.10.10

  make mrproper

  make headers_check
  make INSTALL_HDR_PATH=dest headers_install
  cp -rv dest/include/* /tools/include
}

glibc() {
  maybe_untar_cd glibc-2.18 .tar.xz

  # Book gives a workaround.  My machine doesn't need this.

  #if [ ! -r /usr/include/rpc/types.h ]; then
  #  su -c 'mkdir -p /usr/include/rpc'
  #  su -c 'cp -v sunrpc/rpc/*.h /usr/include/rpc'
  #fi

  sed -i -e 's/static __m128i/inline &/' sysdeps/x86_64/multiarch/strstr.c

  mkdir -v ../glibc-build
  cd ../glibc-build

  ../glibc-2.18/configure                             \
        --prefix=/tools                               \
        --host=$LFS_TGT                               \
        --build=$(../glibc-2.18/scripts/config.guess) \
        --disable-profile                             \
        --enable-kernel=2.6.32                        \
        --with-headers=/tools/include                 \
        libc_cv_forced_unwind=yes                     \
        libc_cv_ctors_header=yes                      \
        libc_cv_c_cleanup=yes

  make
  make install
}

libstdcpp() {
  cd $LFS/sources
  mkdir libstdcpp-build
  cd libstdcpp-build
  ../gcc-4.8.1/libstdc++-v3/configure \
      --host=$LFS_TGT                      \
      --prefix=/tools                      \
      --disable-multilib                   \
      --disable-shared                     \
      --disable-nls                        \
      --disable-libstdcxx-threads          \
      --disable-libstdcxx-pch              \
      --with-gxx-include-dir=/tools/$LFS_TGT/include/c++/4.8.1
  make
  make install
}

# Second pass of binutils.  Compile with the gcc and ar we built.
binutils2() {
  cd $LFS/sources

  maybe_untar_cd binutils-2.23.2 .tar.bz2

  set -o errexit

  # Delete old one
  rm -rf ../binutils-build
  mkdir -v ../binutils-build
  cd ../binutils-build

  CC=$LFS_TGT-gcc                \
  AR=$LFS_TGT-ar                 \
  RANLIB=$LFS_TGT-ranlib         \
  ../binutils-2.23.2/configure   \
      --prefix=/tools            \
      --disable-nls              \
      --with-lib-path=/tools/lib \
      --with-sysroot

  make
  make install

  # Now prepare the linker for the “Re-adjusting” phase in the next chapter: 
  make -C ld clean
  make -C ld LIB_PATH=/usr/lib:/lib
  cp -v ld/ld-new /tools/bin
}

patch_gcc_2() {
  cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
      `dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/include-fixed/limits.h

  cp -v gcc/Makefile.in gcc/Makefile.in.tmp
  sed 's/^T_CFLAGS =$/& -fomit-frame-pointer/' gcc/Makefile.in.tmp \
    > gcc/Makefile.in
}

gcc2() {
  # We're going to patch this source again and rebuild.
  rm -rf $LFS/sources/gcc-4.8.1
  rm -rf $LFS/sources/gcc-build

  maybe_untar_cd gcc-4.8.1 .tar.bz2

  untar_gcc_deps

  set -o errexit

  patch_gcc_2
  patch_gcc_1

  mkdir_cd ../gcc-build

  CC=$LFS_TGT-gcc                                      \
  CXX=$LFS_TGT-g++                                     \
  AR=$LFS_TGT-ar                                       \
  RANLIB=$LFS_TGT-ranlib                               \
  ../gcc-4.8.1/configure                               \
      --prefix=/tools                                  \
      --with-local-prefix=/tools                       \
      --with-native-system-header-dir=/tools/include   \
      --enable-clocale=gnu                             \
      --enable-shared                                  \
      --enable-threads=posix                           \
      --enable-__cxa_atexit                            \
      --enable-languages=c,c++                         \
      --disable-libstdcxx-pch                          \
      --disable-multilib                               \
      --disable-bootstrap                              \
      --disable-libgomp                                \
      --with-mpfr-include=$(pwd)/../gcc-4.8.1/mpfr/src \
      --with-mpfr-lib=$(pwd)/mpfr/src/.libs

  make 
  make install

  ln -sv gcc /tools/bin/cc
}

sanity_check() {
  cd /tmp
  echo 'main(){}' >dummy.c
  cc dummy.c
  readelf -l a.out | grep ': /tools'
}

"$@"
